package com.gdssoft.pdaweb.jobs;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gdssoft.core.util.DateUtil;
import com.gdssoft.pdaweb.dao.CheckWorkOrderDao;
import com.gdssoft.pdaweb.model.CheckWorkOrder;
import com.gdssoft.pdaweb.service.EamService;

public class EamSyncCheckOrderStatusJob {
private transient final Log logger = LogFactory.getLog(getClass());
	
	private EamService eamService;
	private CheckWorkOrderDao checkWorkOrderDao;
	
	public EamSyncCheckOrderStatusJob(EamService eamService, 
			CheckWorkOrderDao checkWorkOrderDao) {
		this.eamService = eamService;
		this.checkWorkOrderDao = checkWorkOrderDao;
	}
	
	public void doJob() {
		logger.info("EamSyncCheckOrderStatusJob execute start");
		
		List<CheckWorkOrder> unSynchroCheckWorkOrdeList 
				= checkWorkOrderDao.listUnSynchroCheckWorkOrder();
		
		for(int i = 0 ; i <unSynchroCheckWorkOrdeList.size(); i++ ) {
			CheckWorkOrder checkWorkOrder = unSynchroCheckWorkOrdeList.get(i);
			Long woId = checkWorkOrder.getAssignmentID();//工单的唯一标识
			boolean bu = eamService.syncCheckOrderStatus(checkWorkOrder);
			if (bu) {
				
				String NowTime = DateUtil.dateFormat(Calendar.getInstance().getTime(), 
						"yyyy-MM-dd HH:mm:ss");// 系统时间
				
				checkWorkOrder.setFeedbackDt(NowTime);
				
				checkWorkOrderDao.saveOrUpdate(checkWorkOrder);
				
				logger.info("成功同步工单唯一标识为：" + woId + "的巡检工单！");
			} else {
					logger.warn("同步工单唯一标识为：" + woId + "的巡检工单失败！");
			}
		}
		logger.info("EamSyncCheckOrderStatusJob execute end");
	}
}
